const targetPage = {
	qyreg: '/pages/register/reg',
	qygate: '/pages2/gate/list',
	qyvisited: '/pages2/visited/list',
	qyrecept: '/pages2/recept/list',
	qymeeting: '/pages1/metting/list/list',
	qyremote: '/pages1/remote/list'
}

const fromType = {
	qyreg: 'qyreg', //注册
	qygate: 'qygate', //门禁管理
	qyvisited: 'qyvisited', //受访者
	qyrecept:'qyrecept', //前台
	qymeeting:'qymeeting', //会议预约
	qyremote:'qyremote' //远程开门
}

const saveOpenUserId = function(userId, openId) {
	uni.setStorageSync('userId', userId)
	uni.setStorageSync('openId', openId)
}

const getOpenUserId = function() {
	let userId = uni.getStorageSync('userId')
	let openId = uni.getStorageSync('openId')
	return {
		userId,
		openId
	}
}

const getStateName = function(value) {
	if (value == '1') {
		return '待接受'
	} else if (value == '2') {
		return '待访问'
	} else if (value == '3') {
		return '已到访'
	} else if (value == '4') {
		return '未到达'
	} else if (value == '5') {
		return '已过期'
	}else if (value == '6') {
		return '已取消'
	} else {
		return ''
	}
}

 const timestampToTime = function(timestamp) {
    timestamp = timestamp ? timestamp : null;
    let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m;
  }


uni.addInterceptor('request', {
	invoke(args) {
		args.header = Object.assign({
			'mToken': getOpenUserId().openId
		}, args.header)

		uni.showLoading({
			title: '请稍后...'
		})
		
		console.log('http request: ' + JSON.stringify(args))
	},
	success(args) {
		console.log('http success: ' + JSON.stringify(args))
	},
	fail(err) {
		console.log('http fail: ' + JSON.stringify(err))
	},
	complete(res) {
		uni.hideLoading()
	}
})

export default {
	// host: 'http://192.168.3.18:10091/api/mobile/',
	// imageHost: 'http://192.168.3.18:10091',
	host: 'https://hnsjgc.com.cn:10081/api/mobile/',
	imageHost: 'https://hnsjgc.com.cn:10081',
	target: targetPage,
	from: fromType,

	saveOpenUserId,
	getOpenUserId,
	getStateName,
	timestampToTime
}